from tkinter import *
from tkinter.messagebox import *


class Window:
    def __init__(self):
        self.frame = Tk()
        self.frame.title("响应键盘事件")
        self.frame.geometry("800x600+300+300")
        self.frame.resizable(0, 0)

        # 绑定键盘事件
        self.frame.bind("<KeyPress>", self.key_method)

    def key_method(self, event):
        # if event.keysym == "Return":
        # showinfo("系统消息","你按了回车键")
        if event.keysym == "F1":
            showinfo("系统消息", "你按了F1")
        if event.keycode == 13:
            showinfo("系统消息", "你按了回车键1")

    def show(self):
        self.frame.mainloop()


if __name__ == "__main__":
    this_window = Window()
    this_window.show()